package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/course/constant"
	"github.com/lab-online/internal/course/entity"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
	"github.com/lab-online/pkg/util"
)

type GetCourseReqParams = GetExperimentsReqParams

var GetCourseValidator = GetExperimentsValidator

type ExperimentMeta struct {
	ExperimentID uint   `json:"id" binding:"required"`    // 实验ID
	Index        uint   `json:"index" binding:"required"` // 实验序号
	Meta         string `json:"meta" binding:"required"`  // 实验附加数据
}
type GetCourseRespData struct {
	SearchCourseRespData
	Experiments    []*SearchExperimentRespData `json:"experiments" binding:"required"`
	ExperimentMeta []*ExperimentMeta           `json:"experimentMeta" binding:"required"`
}
type GetCourseRespBody struct {
	resp.BaseRespBody
	Data GetCourseRespData `json:"data" binding:"required"`
}

// GetCourse 实验详情
//
//	@Summary				实验详情
//	@Description			查询实验详情
//	@Description.markdown	GetCourse
//	@Tags					course
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string	true	"`all`"
//	@Param					id				path		uint	true	"课程id"
//	@Success				200				{object}	GetCourseRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@Failure				403				{object}	resp.FailedRespBodyString
//	@Failure				500				{object}	resp.FailedRespBodyString
//	@x-apifox-status		"developing"
//	@Router					/v1/courses/:id	[get]
func (h *Handler) GetCourse(c *gin.Context) {
	params := c.MustGet(mw.KeyParams).(*GetCourseReqParams)
	courseEntity := entity.NewCourseEntity(entity.WithID(params.ID))

	if course, err := h.domain.GetCourseDetail(courseEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeGetExperimentsSuccess, &GetCourseRespData{
			SearchCourseRespData: SearchCourseRespData{
				ID:          course.GetID(),
				Name:        course.GetName(),
				Description: course.GetDescription(),
			},
			ExperimentMeta: util.SliceMap(course.GetCourseExperimentMeta(),
				func(courseExperiment entity.CourseExperiment) *ExperimentMeta {
					return &ExperimentMeta{
						ExperimentID: courseExperiment.ExperimentID,
						Index:        courseExperiment.Index,
						Meta:         courseExperiment.Meta,
					}
				},
			),
			Experiments: util.SliceMap(course.GetExperiments(),
				func(experiment entity.Experiment) *SearchExperimentRespData {
					return &SearchExperimentRespData{
						ID:          experiment.GetID(),
						Name:        experiment.GetName(),
						Description: experiment.GetDescription(),
					}
				}),
		})
	}
}
